<?php

namespace App\Service\Message\v1_2_1;

use App\Constants\ErrorCode;
use App\Helpers\AliyunUtil;
use App\Helpers\ImUtil;
use App\Helpers\RedisUtil;
use App\Model\Gift;
use App\Model\GiftLog;
use App\Model\Message;
use App\Model\Service\ConsumeService;
use App\Model\Setting\SettingAutoMsg;
use App\Model\User;
use App\Model\UserCoin;
use App\Service\BaseService;
use App\Model\AccostLogs;
/**
 * 搭讪聊天
 */
class SuperChatService extends BaseService
{
    //单向聊天限制
    protected $msgLmt = 3;

    protected function RoVerifyList()
    {
        return [
            'chat_user_id' => 'required|int',
            // 'confirm' => 'required|int',
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
	//$userId = 3900022;
	//$this->userId = $userId;
	$chatUserId = $this->request->input('chat_user_id', 0);
        // $confirm = $this->request->input('confirm', 0);
        $gift = Gift::getInfo(check_dev_env() ? 173 : 173);
	if ($this->checkShowGift()) {
            #添加记录
//            go(function () use($userId,$chatUserId){
//                $expiresTime = time()+86400;
//                AccostLogs::addRecord($userId,$chatUserId,$expiresTime);
//            });

          RedisUtil::set("chat_start_{$userId}_{$chatUserId}",1,86400);
	 //   return $this->setError(ErrorCode::FIRST_SUPER_CHAT, '第一次搭讪', [
         //       'gift_icon' => AliyunUtil::IMG_HOST_V2 . $gift['icon'],
         //       'gift_coin' => $gift['coin'],
         //   ]);
        }

        //需要对方回复领取
        $giftNeedFetch = true;
        $expTime = check_dev_env() ? 60 : 7200;

	//超过2次免费搭讪
	//改成不可免费搭讪
        $freeCheck = GiftLog::checkValidRecord($this->userId, GiftLog::FROM_SUPER_CHAT) < 0;
	 //return $this->setError(ErrorCode::COIN_NOT_ENOUGH, [$myCoin['total_coin'],$gift['coin']]);       
	if (!$freeCheck) {
            //金币不足
            $myCoin = UserCoin::getUserInfo($this->userId);
            if ($myCoin['total_coin'] < $gift['coin']) {
                return $this->setError(ErrorCode::COIN_NOT_ENOUGH, '剩余金币不够送礼, 可以先充值哦~');
            }
        }

	//添加送礼记录
        $record = GiftLog::addSysGiftRecord(
            $this->userId,
            $chatUserId,
            GiftLog::FROM_SUPER_CHAT,
            $giftNeedFetch,
            $gift,
            $expTime
        );

	//用户消费
        if (!$freeCheck) {
            ConsumeService::sendGift($record, $myCoin, false);
        }

        //发送礼物消息
        $gift['gift_type'] = 'superchat';
        Message::sendGift($chatUserId, $this->userId, $gift, $giftNeedFetch, 1, '送出搭讪礼物');

        $accounts = User::getImAccounts([$chatUserId, $this->userId]);
        //发送文字消息
        $msgs = SettingAutoMsg::getList(SettingAutoMsg::TYPE_SYS, 1);
        ImUtil::sendTextMsg($accounts[$chatUserId], $msgs[array_rand($msgs)], $accounts[$this->userId]);

        RedisUtil::set("chat_start_{$userId}_{$chatUserId}",1,86400);
        #添加记录
//        go(function () use($userId,$chatUserId){
//            $expiresTime = time()+86400;
//            AccostLogs::addRecord($userId,$chatUserId,$expiresTime);
//        });

        return [
            'gift_show_image' => $gift['icon'] ? AliyunUtil::IMG_HOST_V2 . $gift['icon'] : '',
            'chat_im_account' => $accounts[$chatUserId],
        ];
    }

    /**
     * 是否显示
     */
    function checkShowGift()
    {
        $key = "super_chat_{$this->userId}";
	return $key;
	if (RedisUtil::get($key) == 1) return 0;
        RedisUtil::set($key, 1, 3600 * 240);
        return 1;
    }
}
